package org.redrider.shop.domain

import grails.converters.*

class InventoryItemController {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def inventoryItems = InventoryItem.list( params )
        withFormat{
            html [ inventoryItemInstanceList: inventoryItems, inventoryItemInstanceTotal: InventoryItem.count() ]
            jason {render inventoryItems as JSON}
        }

    }

    def show = {
        def inventoryItemInstance = InventoryItem.get( params.id )
        withFormat{
            html{
                if(!inventoryItemInstance) {
                    flash.message = "InventoryItem not found with id ${params.id}"
                    redirect(action:list)
                }
                else { return [ inventoryItemInstance : inventoryItemInstance ] }
            }
            json{
                if(!inventoryItemInstance) {
                    render [error:"InventoryItem not found with id ${params.id}"] as JSON
                }
                else { render inventoryItemInstance as JSON }
            }
        }
    }

    def delete = {
        def inventoryItemInstance = InventoryItem.get( params.id )
        def okStatus
        if(inventoryItemInstance) {
            try {
                inventoryItemInstance.delete(flush:true)
                okStatus = "S"

            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                okStatus = "ND"

            }
        }
        else {
            okStatus = "NF"

        }
        withFormat{
            html{
                switch (okStatus){
                    case 'S' : flash.message = "InventoryItem ${params.id} deleted"; redirect(action:list); break
                    case 'ND' : flash.message = "InventoryItem ${params.id} could not be deleted";  redirect(action:show,id:params.id); break
                    case 'NF' : flash.message = "InventoryItem not found with id ${params.id}"; redirect(action:list); break
                }
            }
            json{
                switch (okStatus){
                    case 'S' : render [message:"InventoryItem ${params.id} deleted"] as JSON; break
                    case 'ND' : render [error:"InventoryItem ${params.id} could not be deleted"] as JSON; break
                    case 'NF' : render [error:"InventoryItem not found with id ${params.id}"] as JSON; break
                }
            }
        }
    }

    def edit = {
        def inventoryItemInstance = InventoryItem.get( params.id )
        def okStatus
        if(!inventoryItemInstance) {
            okStatus = 'NF'
        }
        else {
            okStatus = 'S'

        }
        withFormat{
            html{
                switch (okStatus){
                    case 'S' : return [ inventoryItemInstance : inventoryItemInstance ]; break
                    case 'NF' : flash.message = "InventoryItem not found with id ${params.id}"; redirect(action:list); break
                }
            }
            json{
                switch (okStatus){
                    case 'S' : render  as JSON; break
                    case 'NF' : render inventoryItemInstance as JSON; break
                }
            }
        }

    }

    def update = {
        def inventoryItemInstance = InventoryItem.get( params.id )
        def okStatus
        if(inventoryItemInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(inventoryItemInstance.version > version) {

                    inventoryItemInstance.errors.rejectValue("version", "inventoryItem.optimistic.locking.failure", "Another user has updated this InventoryItem while you were editing.")
                    okStatus = 'F'
                }
            }
            inventoryItemInstance.properties = params
            if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save()) {
                okStatus = 'S'
            }
            else {
                okStatus = 'E'
            }
        }
        else {
            okStatus = 'NF'
        }
        withFormat{
            html{
                switch (okStatus){
                    case 'S' : flash.message = "InventoryItem ${params.id} updated"; redirect(action:show,id:inventoryItemInstance.id); break
                    case 'E' : render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]); break
                    case 'F' : render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]); break
                    case 'NF' : flash.message = "InventoryItem not found with id ${params.id}"; redirect(action:list); break
                }
            }
            json{
                switch (okStatus){
                    case 'S' : render inventoryItemInstance as JSON; break
                    case 'E' : render [error:"InventoryItem ${params.id} could not be updated"] as JSON; break
                    case 'E' : render [error:"InventoryItem ${params.id} could not be updated"] as JSON; break
                    case 'NF' : render [error:"InventoryItem not found with id ${params.id}"] as JSON; break
                }
            }
        }

    }

    def create = {
        def inventoryItemInstance = new InventoryItem()
        inventoryItemInstance.properties = params
        withFormat{
            html ['inventoryItemInstance':inventoryItemInstance]
            json {render inventoryItemInstance as JSON}
        }
    }

    def save = {
        def inventoryItemInstance = new InventoryItem(params)
        withFormat{
            html{
                if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save()) {
                    flash.message = "InventoryItem ${inventoryItemInstance.id} created"
                    redirect(action:show,id:inventoryItemInstance.id)
                }
                else {
                    render(view:'create',model:[inventoryItemInstance:inventoryItemInstance])
                }
            }
            json{
                if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save()) {
                    render [error:"InventoryItem ${inventoryItemInstance.id} created"] as JSON
                }
                else {
                    render inventoryItemInstance as JSON
                }
            }
        }

    }
}
